/* Copyright (C)
 * 2013 - Frank
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "platform.h"

typedef struct _s2b
{
    char int8;
    short int16;
    int int32;
}small2Big;

typedef struct _b2s
{
    int int32;
    short int16;
    char int8;

}big2Small;

typedef struct _usort
{
    char int8;
    int int32;
    short int16;
}unsorted;

typedef struct _pStruct
{
    char* pint8;
    short* pint16;
    int* pint32;
}pStruct;

int array_int[100]={};
char array_char[100]={};

void descript_sizeof(void)
{
    printf("SIZEOF() is a macro for calculate memory usage for var!\n");
    printf("--Important rule:\n");
    printf("1. sizeof() only calculate COMPILE state can be fixed memory size.\n");
}

void descript_align(void)
{
    printf("ALIGN must be the biggest struct member's size int count\n");
}

void calc_sizeof(void)
{
    int* pMalloc;
    pMalloc = malloc(100);
    memset(pMalloc,0x0,sizeof(4*100));

    printf("\n--------------------------------------------------------\n");
    printf("On this platform, sizeof() usage:\n");
    printf("sizeof(long):%d\n",sizeof(long));
    printf("sizeof(int):%d\n",sizeof(int));
    printf("sizeof(char):%d\n",sizeof(char));
    printf("sizeof(short):%d\n",sizeof(short));
    printf("sizeof(int*):%d\n",sizeof(int*));
    printf("sizeof(int array[100]):%d\n",sizeof(array_int));
    printf("sizeof(char array[100]):%d\n",sizeof(array_char));
    printf("***sizeof(malloc(100)):%d\n",sizeof(pMalloc));

    printf("\n--------------------------------------------------------\n");
    printf("SIZEOF STRUTC\n");
    printf("sizeof(big2Small):%d\n",sizeof(big2Small));
    printf("\
    typedef struct _b2s \n\
    {                   \n\
        int int32;      \n\
        char int8;      \n\
        short int16;    \n\
    }big2Small;\n");

    printf("\nsizeof(small2Big):%d\n",sizeof(small2Big));
    printf("\
    typedef struct _b2s \n\
    {                   \n\
        char int8;      \n\
        short int16;    \n\
        int int32;      \n\
    }big2Small;\n");

    printf("\nsizeof(unsorted):%d\n",sizeof(unsorted));
    printf("\
    typedef struct _b2s \n\
    {                   \n\
        char int8;      \n\
        int int32;      \n\
        short int16;    \n\
    }big2Small;\n");

    printf("\nsizeof(pStruct):%d\n",sizeof(pStruct));
    printf("\
    typedef struct _b2s \n\
    {                   \n\
        char* pint8;      \n\
        short* pint16;    \n\
        int* pint32;      \n\
    }big2Small;\n");
}

#ifdef __MAIN__
int main(int argc,char** argv)
{
    descript_sizeof();
    descript_align();
    calc_sizeof();

    return 1;
}
#endif
